package mysql.swing.JFrame;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import mysql.swing.MyConnect;
import mysql.swing.Student;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class EditFrame extends JFrame implements ActionListener{

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JButton btnOk;
	public static int check = 1;
	MyConnect myconnect = new MyConnect();

	/**
	 * Create the frame.
	 */
	public EditFrame() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setResizable(false);
		setLocationRelativeTo(null);

		setBounds(100, 100, 370, 232);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.NORTH);

		if (MyFrame.k == 1) {
			JLabel lblDatabase = new JLabel("Update Database");
			panel.add(lblDatabase);
		} else {
			JLabel lblDatabase = new JLabel("Insert Database");
			panel.add(lblDatabase);
		}

		JPanel panel_1 = new JPanel();
		contentPane.add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(null);

		JLabel lblId = new JLabel("ID");
		lblId.setBounds(47, 11, 46, 14);
		panel_1.add(lblId);

		textField = new JTextField();
		textField.setBounds(155, 8, 154, 20);
		panel_1.add(textField);
		textField.setColumns(10);

		JLabel lblName = new JLabel("Name");
		lblName.setBounds(47, 52, 46, 14);
		panel_1.add(lblName);

		textField_1 = new JTextField();
		textField_1.setBounds(155, 49, 154, 20);
		panel_1.add(textField_1);
		textField_1.setColumns(10);

		JLabel lblPoint = new JLabel("Point");
		lblPoint.setBounds(47, 100, 46, 14);
		panel_1.add(lblPoint);

		textField_2 = new JTextField();
		textField_2.setBounds(155, 97, 154, 20);
		panel_1.add(textField_2);
		textField_2.setColumns(10);

		JPanel panel_2 = new JPanel();
		contentPane.add(panel_2, BorderLayout.SOUTH);
		if (MyFrame.k == 1) {
			btnOk = new JButton("Update");
			panel_2.add(btnOk);
			textField.setText(MyFrame.id);
			textField_1.setText(MyFrame.name);
			textField_2.setText(MyFrame.point);
		} else {
			btnOk = new JButton("Insert");
			panel_2.add(btnOk);
		}

		btnOk.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				action();
			}
		});

		JButton btnCancel = new JButton("Clear");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				learAll();
			}
		});
		panel_2.add(btnCancel);
	}

	public void learAll() {
		// TODO Auto-generated method stub
		textField.setText("");
		textField_1.setText("");
		textField_2.setText("");
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
	}

	private void action() {
		Student s = new Student();
		s.setId(textField.getText());
		s.setName(textField_1.getText());
		s.setPoint(Float.parseFloat(textField_2.getText()));
		System.out.println(s.getId() + " " + s.getName() + " "
				+ s.getPoint() + " " + MyFrame.id);

		if (MyFrame.k == 1) {
			myconnect.connect();
			myconnect.update(MyFrame.id, s);
		} else {
			myconnect.connect();
			myconnect.insert(s);
		}
	}

}
